package exp7;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
class MyFrame extends JFrame{
	public MyFrame(String title) {
		setLayout(new BorderLayout());
		setTitle(title);
		setSize(800,400);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
	}
	void showMe() {
		setVisible(true);
	}
}

class EventListener extends MouseAdapter{
	private JLabel target=new JLabel("t");
	Graphics g =null;
	int x1,y1;
	@Override
	public void mousePressed(MouseEvent e) {
		x1=e.getX();
		y1=e.getY();
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		g=target.getGraphics();
		g.setColor(Color.RED);
		g.clearRect(x1, y1,e.getX()-x1 ,e.getY()-y1);
		g.drawRect(x1,y1,e.getX()-x1 ,e.getY()-y1);
	}

	void init() {
		MyFrame f = new MyFrame("Test");
		target.addMouseMotionListener(this);
		target.addMouseListener(this);
		f.add(target);
		f.showMe();
		
		
	}
}
public class Test {
	public static void main(String[] args) {
		new EventListener().init();
	}
	
}
